﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Text;
using BE;


namespace VentaPorCatalogos
{
    public class Page : System.Web.UI.Page
    {
        protected enum behavior { Initial, Edit, New, View };
        protected const Int32 cPageSize = 10;

        protected void HabilitarControles(bool pEstado)
        {
            EnableDisableInputControls(this.Controls, pEstado);            
        }

        protected void HabilitarControles(bool pEstado, ControlCollection pControlCollection)
        {
            EnableDisableInputControls(pControlCollection, pEstado);
            FindOtherControls(pControlCollection).ForEach(x => x.Enabled = pEstado);
        }

        protected void BlanquearControles()
        {
            BlanquearControles(this.Controls);
        }

        protected void BlanquearControles(ControlCollection cc)
        {            
            foreach (Control c in cc)
            {
                if (c is System.Web.UI.WebControls.TextBox)
                    (c as System.Web.UI.WebControls.TextBox).Text = string.Empty;
                if (c is System.Web.UI.WebControls.Label && string.IsNullOrEmpty((c as System.Web.UI.WebControls.Label).AssociatedControlID))
                    (c as System.Web.UI.WebControls.Label).Text = "0";
                if (c.GetType() == typeof(System.Web.UI.WebControls.Image))
                    (c as System.Web.UI.WebControls.Image).ImageUrl = string.Empty;
                else if (c.Controls.Count > 0)
                    BlanquearControles(c.Controls);
            }            
        }

        private void EnableDisableInputControls(ControlCollection cc, bool pHabilitado)
        {            
            foreach (Control c in cc)
            {
                if (c is System.Web.UI.WebControls.TextBox)
                    (c as System.Web.UI.WebControls.TextBox).ReadOnly = !pHabilitado;
                if (c is System.Web.UI.WebControls.DropDownList)
                    (c as System.Web.UI.WebControls.DropDownList).Enabled = pHabilitado;
                if (c is System.Web.UI.WebControls.CheckBox)
                    (c as System.Web.UI.WebControls.CheckBox).Enabled = pHabilitado;
                else if (c.Controls.Count > 0)
                    EnableDisableInputControls(c.Controls, pHabilitado);

            }            
        }

        private List<System.Web.UI.WebControls.WebControl> FindOtherControls(ControlCollection cc)
        {
            List<System.Web.UI.WebControls.WebControl> _Controls = new List<System.Web.UI.WebControls.WebControl>();
            foreach (Control c in cc)
            {
                if (c is System.Web.UI.WebControls.Button)
                    _Controls.Add(c as System.Web.UI.WebControls.Button);
                if (c is System.Web.UI.WebControls.ImageButton)
                    _Controls.Add(c as System.Web.UI.WebControls.ImageButton);
                if (c is System.Web.UI.WebControls.FileUpload)
                    _Controls.Add(c as System.Web.UI.WebControls.FileUpload);
                else if (c.Controls.Count > 0)
                    _Controls.AddRange(FindOtherControls(c.Controls));

            }
            return _Controls;
        }

        protected behavior GetFormBehaviour()
        {
            return (behavior)ViewState["FormBehavior"];
        }

        protected void SetFormBehaviour(behavior pBehaviour)
        {
            ViewState.Add("FormBehavior", pBehaviour);
        }


        protected T GetModel<T>() where T : class
        {
            T oModel = null;
            if (Session["Model"] != null)
            {
                oModel = (T)Session["Model"];
            }
            return oModel;
        }

        protected void SetModel<T>(T pModel)
        {
            Session["Model"] = pModel;
        }

        protected void EjecutarScript(string pScript)
        {
            StringBuilder sbScript = new StringBuilder();            
            sbScript.AppendLine(pScript);            

            ScriptManager.RegisterStartupScript(this, this.GetType(), "ScriptASPNET", sbScript.ToString(), true);

        }

        protected void EjecutarScriptJqueryReady(string pScript)
        {
            StringBuilder sbScript = new StringBuilder();
            sbScript.AppendLine(@"$(function () {");
            sbScript.AppendLine(pScript);
            sbScript.AppendLine(@"});");

            ScriptManager.RegisterStartupScript(this, this.GetType(), "ScriptASPNET", sbScript.ToString(), true);

        }

        protected void MostrarMensaje(string pMensaje)
        {
            EjecutarScript(string.Format("alert(\"{0}\")", pMensaje));
        }


        protected String GetPerfil()
        {
            String wPerfil = string.Empty;
            UsuarioBE oUsuarioBE = (UsuarioBE)Session["User"];
            if (oUsuarioBE != null)
                wPerfil = oUsuarioBE.Perfil;

            return wPerfil;
        }

        
    }
}